/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.IOException;
import java.util.ArrayList;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.EditorView;
import net.worlds.Main;
import net.worlds.PropertyDef;
import net.worlds.Util;
import net.worlds.component.CommandBar;
import net.worlds.ui.UI;
import se.krka.kahlua.vm.LuaClosure;


public final class EditorOverlay extends EMain{
    public PresetsFrame presetsFrame;

    public EButton addRemovePlayerItem;
    //public EButton showHideToolsItem;
    public EButton showHideConsoleItem;
    public EButton presetsButton;
   
    public EStickySwingFrame commandBar;
    public EFrame helpFrame;
    public EFrame newFrame;

    private CommandBar commandBarComponent = new CommandBar();
    private ELeaderboard leaderboard;
    private EFrame actionButtonFrame;
    
    private int actionY = 45;
    private ArrayList<EButton> actionButtons = new ArrayList<EButton>(4);

    public EditorOverlay(EditorView view, int width, int height)
    {
        super(width, height);
        final EditorTop top = (EditorTop) view.getTop();
        presetsFrame = new PresetsFrame(this);
        presetsFrame.visible = false;
        try {
            /*showHideToolsItem = new EButton(AssetManager.loadImage("tools.png", "tools.png"));
            showHideToolsItem.setTooltip("Show/hide advanced tools");
            showHideToolsItem.setUndecorated(true);
            showHideToolsItem.setWidth(32);
            showHideToolsItem.setHeight(32);
            showHideToolsItem.setY(200);
            showHideToolsItem.setActionListener(new EActionListener() {

                @Override
                public void actionPerformed() {
                    if (UI.advancedToolsVisible) {
                        hideAdvancedTools();
                    } else {
                        showAdvancedTools();
                    }
                }
            });
            addChild(1, showHideToolsItem);*/
            showHideConsoleItem = new EButton(AssetManager.loadImage("lua.png", "lua.png"));
            showHideConsoleItem.setTooltip("Show/hide scripting console");
            showHideConsoleItem.setUndecorated(true);
            showHideConsoleItem.setWidth(32);
            showHideConsoleItem.setHeight(32);
            showHideConsoleItem.setY(200);
            showHideConsoleItem.setActionListener(new EActionListener() {

                @Override
                public void actionPerformed() {
                    if (commandBar.visible) {
                        commandBar.setVisible(false);
                    } else {
                        commandBar.setVisible(true);
                    }
                    UI.repaintAdvancedTools = true;
                }
            });
            addChild(1, showHideConsoleItem);
            addRemovePlayerItem = new EButton(AssetManager.loadImage("addplayer.png", "addplayer.png"));
            addRemovePlayerItem.setTooltip("Add/remove character");
            addRemovePlayerItem.setUndecorated(true);
            addRemovePlayerItem.setWidth(32);
            addRemovePlayerItem.setHeight(32);
            addRemovePlayerItem.setY(150);
            addRemovePlayerItem.setActionListener(new EActionListener() {

                @Override
                public void actionPerformed() {
                    if(Main.isServer())
                        return;
                    if (Main.getLocalPlayer() == null) {
                        Main.addPlayer(Main.getLoginName());
                    } else {
                        Main.removePlayer();
                    }
                }
            });
            addChild(1, addRemovePlayerItem);
            presetsButton = new EButton(AssetManager.loadImage("addpackage.png", "addpackage.png"));
            presetsButton.setTooltip("Add premade item");
            presetsButton.setUndecorated(true);
            presetsButton.setWidth(32);
            presetsButton.setHeight(32);
            presetsButton.setY(250);
            presetsButton.setActionListener(new EActionListener() {

                @Override
                public void actionPerformed() {
                    if(presetsFrame.visible)
                    {
                        presetsFrame.visible = false;
                    }
                    else
                    {
                        System.out.println("To add basic shapes, click the Advanced Tools button and use the appropriate toolbar buttons.");
                        presetsFrame.visible = true;
                    }
                }
            });

            addChild(1, presetsButton);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        

        commandBar = new EStickySwingFrame(this, 700, 40, -1, 0, 0, -1, commandBarComponent);
        commandBar.setUndecorated(true);
        commandBar.setBackgroundColor(new Color(192, 192, 192));
        addChild(1, commandBar);
        
        helpFrame = new EFrame(this, 200, 125, 400, 250);
        helpFrame.addTitle("2DWorlds Help");
        ELabel controlsLabel = new ELabel("Editor Controls");
        controlsLabel.setColor(new Color(197, 203, 217));
        controlsLabel.setFont(new Font("Arial", Font.BOLD, 16));
        helpFrame.addChild(1, controlsLabel);
        controlsLabel.setX(6);
        controlsLabel.setY(28);
        /*String[] lines = {"* a, left arrow - move left",
        "* d, right arrow - move right",
        "* shift - run",
        "* w, up arrow - jump",
        "* 1-9 - select tool",
        "* Q - drop tool (if applicable)"};*/
        String[] lines = {"* left mouse button- select",
        "* right mouse button - move camera",
        "* mouse wheel or (+), (-) - zoom",
        "* shift - multi select",
        "* delete - delete selection"};
        int y = 47;
        Font lineFont = new Font("Arial", -1, 13);
        for(String l : lines)
        {
            ELabel lineLabel = new ELabel(l);
            lineLabel.setFont(lineFont);
            lineLabel.setX(6);
            lineLabel.setY(y);
            helpFrame.addChild(1, lineLabel);
            y += 17;
        }
        helpFrame.visible = false;
        
        addChild(7, helpFrame);
        leaderboard = new ELeaderboard(this, 10, 60);
        
        actionButtonFrame = new EStickyFrame(this, 32, -1, 0, -1, 55, 0);
        actionButtonFrame.clickThrough = true;
        actionButtonFrame.setUndecorated(true);
        actionButtonFrame.setBackgroundColor(new Color(0, 0, 0, 0));
        addChild(0, actionButtonFrame);
    }
    public void showNewFrame()
    {
        if(newFrame != null && newFrame.visible)
            return;
        if(hasChild(1, newFrame))
            this.removeChild(newFrame);
        newFrame = new EStickyFrame(this, -1, -1, 40, 10, 60, 10);
        newFrame.visible = true;
        newFrame.addTitle("New Game", new Runnable()
        {
            @Override
            public void run() {
                //showAdvancedTools();
            }
        });
        
        ELabel sideLabel = new ELabel("Side-scrolling");
        sideLabel.setColor(new Color(197, 203, 217));
        sideLabel.setFont(new Font("Arial", Font.BOLD, 16));
        sideLabel.setX(6);
        sideLabel.setY(30);
        newFrame.addChild(1, sideLabel);
        
        addTemplate(10, 50, "Classic");
        addTemplate(240, 50, "Big");
        addTemplate(470, 50, "Tall");
        addTemplate(10, 190, "Islands");
        addTemplate(240, 190, "Moon");
        addTemplate(470, 190, "Ocean");
        
        ELabel topLabel = new ELabel("Top down");
        topLabel.setColor(new Color(197, 203, 217));
        topLabel.setFont(new Font("Arial", Font.BOLD, 16));
        topLabel.setX(6);
        topLabel.setY(330);
        newFrame.addChild(1, topLabel);
        addTemplate(10, 350, "Driving");
        addTemplate(240, 350, "Empty");
    }
    
    private void addTemplate(int x, int y, final String name)
    {
        try {
            EButton b = new EButton(AssetManager.loadImage("Template_"+name, "resource://templates/"+name+".jpg"), 210, 120);
            b.setTooltip(name.replace("_", " "));
            b.setX(x);
            b.setY(y);
            newFrame.addChild(1, b);
            b.setActionListener(new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    if(Main.loading)
                        return;
                    try {
                        Main.newGame();
                        Main.getGame().setMainWorld();
                        Main.getGame().stop(true);
                        Main.loadFromResource("/net/resources/templates/"+name, true);
                    } catch (IOException ex) {
                        Main.newGame();
                        Main.getGame().setMainWorld();
                    }
                    System.gc();
                    newFrame.visible = false;
                }
            });
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    public void addActionButton(final LuaClosure c, String img, String tooltip)
    {
        EButton btn;
        try {
            btn = new EButton(AssetManager.loadImage(img, img));
        } catch (IOException ex) {
        return;}
        btn.setTooltip(tooltip);
        btn.setUndecorated(true);
        btn.setWidth(32);
        btn.setHeight(32);
        btn.setY(actionY);
        btn.setX(0);
        btn.setActionListener(new EActionListener()
        {
            @Override
            public void actionPerformed() {
                Main.getScriptContext().call(c);
            }  
        });
        actionButtonFrame.addChild(1, btn);
        actionButtons.add(btn);
        actionY += 50;
    }
    public void clearActionButtons()
    {
        for(EButton btn : actionButtons)
            actionButtonFrame.removeChild(btn);
        actionButtons.clear();
        actionY = 95;
    }
    @Override
    public boolean eMouseDown(int b, int x, int y)
    {
        boolean r = super.eMouseDown(b, x, y);
        if(x > getWidth() - 220)
            UI.repaintAdvancedTools = true;
        return r;
    }
    @Override
    public boolean eMouseUp(int b, int x, int y)
    {
        boolean r = super.eMouseUp(b, x, y);
        if(x > getWidth() - 220)
            UI.repaintAdvancedTools = true;
        return r;
    }
    
    @Override
    public void setMouseComponent(EComponent c)
    {
        EComponent old = mouseComponent;
        super.setMouseComponent(c);
        if(mouseComponent != old)
            UI.repaintAdvancedTools = true;
    }
}
